<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;

class Settle extends Base
{
    protected $unionSettleTable = 'union_settle';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    //添加公会结算记录
    public function insertUnionSettle($data)
    {
        return DB::table($this->unionSettleTable)->insertGetId($data);
    }

    //更新公会结算记录
    public function updateUnionSettle($id, $data=[], $where=[])
    {
        if(empty($id)) {
            return 0;
        }

        $whereData = [];
        $whereData['id'] = (int) $id;
        if(isset($where['union_id'])) {
            $whereData['union_id'] = (int) $where['union_id'];
        }
        if(isset($where['user_id'])) {
            $whereData['user_id'] = (int) $where['user_id'];
        }

        $upData = [];
        if(!empty($data['settle_docx'])) {
            $upData['settle_docx'] = (int) $data['settle_docx'];
        }
        if(!empty($data['confirm_img'])) {
            $upData['confirm_img'] = $data['confirm_img'];
        }
        if(!empty($data['invoice_img'])) {
            $upData['invoice_img'] = $data['invoice_img'];
        }
        if(!empty($data['audit_msg'])) {
            $upData['audit_msg'] = $data['audit_msg'];
        }
        if(!empty($data['status'])) {
            $upData['status'] = $data['status'];
        }
        if(!empty($upData)) {
            DB::table($this->unionSettleTable)
                ->where($whereData)
                ->update($upData);
        }

        return 1;
    }

    /**
     * 获取公会结算记录
     */
    public function getUnionSettleById($id)
    {
        if(empty($id)) {
            return [];
        }
        $result = DB::table($this->unionSettleTable)->where('id', $id)->first();
        if(empty($result)) {
            return [];
        }
        return $this->objToArray($result);
    }

    /**
     * 获取公会结算记录根据结算周期公会ID
     */
    public function getUnionSettleByUnionIdDate($unionId, $dateSt, $dateEd)
    {
        $result = DB::table($this->unionSettleTable)->where('union_id', $unionId)->where('settle_date_st', $dateSt)->where('settle_date_ed', $dateEd)->first();
        if(empty($result)) {
            return [];
        }
        return $this->objToArray($result);
    }



}
